package com.youi.service.impl;

import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.dao.GoodsDao;
import com.youi.entity.Goods;
import com.youi.entity.Goodsimgs;
import com.youi.entity.Goodssize;
import com.youi.entity.Merchant;
import com.youi.service.GoodsService;
import com.youi.service.GoodsimgsService;
import com.youi.service.GoodssizeService;
import com.youi.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl extends BaseServiceImpl<Goods> implements GoodsService{
		
		private GoodsDao goodsDao;
		@Autowired
		private MerchantService merchantService;
		@Autowired
		private GoodsimgsService goodsimgsService;
		@Autowired
		private GoodssizeService goodssizeService;
		@Autowired
		public void setfaultRecordDao(GoodsDao goodsDao){
			this.goodsDao=goodsDao;
			super.setBaseDAO(goodsDao);
		}

		@Override
		public List<Goods> getGoodsList(Map<String, Object> params, Map<String, Object> searchParams,
				Map<String, Object> keyParams, Map<String, Object> sortParams, Integer pageIndex, Integer pageNum,
				Map<String, Object> gtParams, Map<String, Object> ltParams) {
			// TODO Auto-generated method stub
			return goodsDao.getGoodsList(params, searchParams, keyParams, sortParams, pageIndex, pageNum, gtParams, ltParams);
		}

		@Override
		public void getGoodsMsg(List<Goods> goodsList) {
			// TODO Auto-generated method stub
			if (goodsList!=null&&goodsList.size()>0) {
				for (Goods goods : goodsList) {
					//获取商品说属商家信息
					if (goods.getMerchantId()!=null) {
						Merchant merchant=merchantService.get(goods.getMerchantId());
						goods.setMerchantName(merchant.getName());
					}
					//获取商品图库
					Map<String, Object> params=new HashMap<String, Object>();
					params.put(Common.DELSTATUS, DelStatus.NODEL);
					params.put("goodsId", goods.getId());
					Map<String, Object> sortPram=new HashMap<String, Object>();
					sortPram.put(Common.ID, Common.DESC);
					List<Goodsimgs> goodsimgsList=goodsimgsService.getByParams(params, null, sortPram);
					goods.setGoodsimgsList(goodsimgsList);
					List<Goodssize> goodssizeList=goodssizeService.getByParams(params);
					goods.setGoodssizeList(goodssizeList);
					Integer allStockNum=0;
					for (Goodssize goodssize : goodssizeList) {
						if (goodssize.getRealStockNum()!=null) {
							allStockNum+=goodssize.getRealStockNum();
							if (goodssize.getStockNum()!=null&&goodssize.getSaleNum()!=null) {
								goodssize.setRealStockNum(goodssize.getStockNum()-goodssize.getSaleNum());
							}							
							goodssizeService.saveOrUpdate(goodssize);
						}
					}
					goods.setAllStockNum(goods.getRealStockCnt()-goods.getRealSaleCnt());
					//商品购买记录
//					List<Purchasehistory> purchasehistoryList=purchasehistoryService.getByParams(params);
//					goods.setPurchasehistoryList(purchasehistoryService.getPurchasehistoryMsg(purchasehistoryList));
				}
			}
		}

		@Override
		public void getGoodsMsg(Goods goods) {
			// TODO Auto-generated method stub
			if (goods!=null) {
				//获取商品说属商家信息
				Merchant merchant=merchantService.get(goods.getMerchantId());
				goods.setMerchantName(merchant.getName());
				Map<String, Object> params=new HashMap<String, Object>();
				params.put(Common.DELSTATUS, DelStatus.NODEL);
				params.put("goodsId", goods.getId());
				Map<String, Object> sortPram=new HashMap<String, Object>();
				sortPram.put(Common.ID, Common.DESC);
				List<Goodsimgs> goodsimgsList=goodsimgsService.getByParams(params, null, sortPram);
				goods.setGoodsimgsList(goodsimgsList);
				List<Goodssize> goodssizeList=goodssizeService.getByParams(params);
				goods.setGoodssizeList(goodssizeList);
				Integer allStockNum=0;
				if (goodssizeList!=null&&goodssizeList.size()>0) {
					for (Goodssize goodssize : goodssizeList) {
						if (goodssize.getRealStockNum()==null) {
							goodssize.setRealStockNum(0);
						}
						allStockNum+=goodssize.getRealStockNum();
						if (goodssize.getSaleNum()==null) {
							goodssize.setSaleNum(0);
						}
						if (goodssize.getStockNum()!=null) {
							goodssize.setRealStockNum(goodssize.getStockNum()-goodssize.getSaleNum());
						}	
						goodssizeService.saveOrUpdate(goodssize);
					}
				}
				goods.setAllStockNum(goods.getRealStockCnt()-goods.getRealSaleCnt());
			}
		}

		@Override
		public Goods getForUpdate(Long id) {
			// TODO Auto-generated method stub
			return goodsDao.getForUpdate(id);
		}

	@Override
	public List<Goods> getGoodsListNotInIds(Map<String, Object> params, Map<String, Object> noParams, Map<String, Object> searchParams, Map<String, Object> keyParams, Map<String, Object> sortParams, Integer pageIndex, Integer pageNum, Map<String, Object> gtParams, Map<String, Object> ltParams) {
		return goodsDao.getGoodsListNotInIds(params,noParams,searchParams,keyParams,sortParams,pageIndex,pageNum,gtParams,ltParams);
	}


}